#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <cstring>
#include <arpa/inet.h>
#include <ctype.h>
#include "stdShared.h"
#include <pthread.h>
#include <json-c/json.h>
using namespace std;

#define PORT 8889
#define BUFFER_SIZE 64

struct UserInfo
{
    UserInfo() {}
    UserInfo(int type, char *name, char *passwd)
    {
        m_type = type;
        strcpy(m_name, name);
        strcpy(m_passwd, passwd);
    }
    char m_name[BUFFER_SIZE];
    char m_passwd[BUFFER_SIZE];
    int m_type; // 业务类型
};

/*线程函数*/
void *thread_func1(void *arg)
{
    /*通信句柄*/
    int connfd = *(int *)arg;
    /*线程分离*/
    pthread_detach(pthread_self());

    char buffer[BUFFER_SIZE] = {0};
    /*服务器接收信息*/
    UserInfo info;
    while (1)
    {
        int readBytes = read(connfd, &buffer, sizeof(buffer));
        if (readBytes > 0)
        {
            cout << "buffer:" << buffer << endl;

            json_object *jsonObj = json_tokener_parse(buffer);

            if (jsonObj == NULL)
            {
                cout << "解析失败" << endl;
            }
            else
            {

                int type = json_object_get_int(json_object_object_get(jsonObj, "type"));
                const char *username = json_object_get_string(json_object_object_get(jsonObj, "username"));
                const char *passwd = json_object_get_string(json_object_object_get(jsonObj, "passwd"));

                cout << "type: " << type << ", username: " << username << ", passwd: " << passwd << endl;
#if 0
            int type = info.m_type;
            if (type == REGISTER)
            {
                cout << "欢迎注册" << endl;
                cout << "username:" << info.m_name << ",passwd:" << info.m_passwd << endl;
            }
            else if (type == LOGIN)
            {
                cout << "欢迎登录" << endl;
                cout << "username:" << info.m_name << ",passwd:" << info.m_passwd << endl;
            }
#endif
            }
        }
        else if (readBytes == 0)
        {

            cout << "客户端下线" << endl;
            break;
        }
        else
        {
            cout << "readBytes < 0" << endl;
        }
        sleep(10);
    }
    /*回收文件描述符*/
    close(connfd);
    /*线程退出*/
    pthread_exit(NULL);
}

int main()
{
    /*创建通信文件描述符*/
    int sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd == -1)
    {
        perror("socket create error");
        _exit(-1);
    }

    /*设置地址复用*/
    int optVal = 1;
    int ret1 = setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &optVal, sizeof(optVal));
    if (ret1 == -1)
    {
        perror("setsockopt error:");
        _exit(-1);
    }

    /*绑定服务器的ip和端口信息*/
    sockaddr_in localAddress;
    /*清除脏数据*/
    memset(&localAddress, 0, sizeof(localAddress));
    /*地址族*/
    localAddress.sin_family = AF_INET; // ipv4
    /* 端口 */
    localAddress.sin_port = htons(PORT);       // 大小端转换
    localAddress.sin_addr.s_addr = INADDR_ANY; // 允许接收来自任意IP地址的连接

    int ret = bind(sockfd, (const sockaddr *)&localAddress, sizeof(localAddress));
    if (ret == -1)
    {
        perror("bind error:");
        _exit(-1);
    }

    /*监听*/
    ret = listen(sockfd, 10);
    if (ret == -1)
    {
        perror("listen error:");
        _exit(-1);
    }

    cout << "等待客户端连接..." << endl;

    while (1)
    {

        int connfd = accept(sockfd, NULL, NULL);
        if (connfd == -1)
        {
            perror("accept error:");
            continue;
        }
        cout << "客户端连接成功,connfd:" << connfd << endl;

        /*创建线程*/
        pthread_t tid;
        int ret = pthread_create(&tid, NULL, thread_func1, (void *)&connfd);
        if (ret != 0)
        {
            perror("thread create error");
            exit(-1);
        }
        /*手动休眠 1s*/
        sleep(1);
    }

    close(sockfd);
    return 0;
}
